#Boa:Frame:pesqFornFrame

import wx
from CONTROLE import *

def create(parent):
    return pesqFornFrame(parent)

[wxID_PESQFORNFRAME, wxID_PESQFORNFRAMELIMPAR, wxID_PESQFORNFRAMELISTBOX1, 
 wxID_PESQFORNFRAMEPESQUISA, wxID_PESQFORNFRAMESTATICTEXT1, 
 wxID_PESQFORNFRAMESTATICTEXT2, wxID_PESQFORNFRAMESTATICTEXT3, 
 wxID_PESQFORNFRAMETXTPESQUISA, 
] = [wx.NewId() for _init_ctrls in range(8)]

class pesqFornFrame(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_PESQFORNFRAME, name=u'pesqFornFrame',
              parent=prnt, pos=wx.Point(563, 307), size=wx.Size(545, 209),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Pesquisa de Fornecedor')
        self.SetClientSize(wx.Size(529, 171))

        self.Pesquisa = wx.Button(id=wxID_PESQFORNFRAMEPESQUISA,
              label=u'Pesquisa', name=u'Pesquisa', parent=self,
              pos=wx.Point(360, 8), size=wx.Size(75, 23), style=0)
        self.Pesquisa.Bind(wx.EVT_BUTTON, self.OnPesquisaButton,
              id=wxID_PESQFORNFRAMEPESQUISA)

        self.limpar = wx.Button(id=wxID_PESQFORNFRAMELIMPAR, label=u'Limpar',
              name=u'limpar', parent=self, pos=wx.Point(448, 8),
              size=wx.Size(75, 23), style=0)
        self.limpar.Bind(wx.EVT_BUTTON, self.OnLimparButton,
              id=wxID_PESQFORNFRAMELIMPAR)

        self.txtPesquisa = wx.TextCtrl(id=wxID_PESQFORNFRAMETXTPESQUISA,
              name=u'txtPesquisa', parent=self, pos=wx.Point(16, 8),
              size=wx.Size(328, 21), style=0, value=u'')

        self.listBox1 = wx.ListBox(choices=[], id=wxID_PESQFORNFRAMELISTBOX1,
              name='listBox1', parent=self, pos=wx.Point(16, 64),
              size=wx.Size(504, 104), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBox1ListboxDclick,
              id=wxID_PESQFORNFRAMELISTBOX1)

        self.staticText1 = wx.StaticText(id=wxID_PESQFORNFRAMESTATICTEXT1,
              label=u'Codigo', name='staticText1', parent=self, pos=wx.Point(16,
              48), size=wx.Size(34, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_PESQFORNFRAMESTATICTEXT2,
              label=u'Descricao', name='staticText2', parent=self,
              pos=wx.Point(104, 48), size=wx.Size(47, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_PESQFORNFRAMESTATICTEXT3,
              label=u'Telefone', name='staticText3', parent=self,
              pos=wx.Point(368, 48), size=wx.Size(43, 13), style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnPesquisaBtnButton(self, event):
        self.listBox1.Clear()
        nome = self.txtPesquisa.GetValue()
        c = Controle()
        lista = c.procurar_fornecedor_nome(nome)
        
        def codd(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            
        def desc(x):
            x = str(x)
            while len(x)<80:
                x = x+' '
            return x
            
        def tel(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x

        for r in lista:
            addd = codd(+r[0])+desc(r[1])+tel(r[2])
            self.listBox1.Append(addd)
                   
    def OnLimparBtnButton(self, event):
        self.txtPesquisa.SetValue('')
        self.listBox1.Clear()

    def OnListBox1ListboxDclick(self, event):
        a = self.listBox1.GetStringSelection()
        cod = a[0:2]
        cod = cod.split(" ")
        cod = str(cod[0])
        
        c = Controle()
        atributos = c.procurar_fornecedor_codigo(cod)
        
        cadFornFrame.textCtrl2.SetValue(str(atributos[0]))
        cadFornFrame.textCtrl3.SetValue(str(atributos[1]))
                
        self.Close()
        #self.alterar_campos(False)

    def OnPesquisaBtnButton(self, event):
        self.listBox1.Clear()
        nome = self.txtPesquisa.GetValue()
        c = Controle()
        lista = c.procurar_fornecedor_nome(nome)
        
        def codd(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            
        def desc(x):
            x = str(x)
            while len(x)<80:
                x = x+' '
            return x
            
        def tel(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x

        for r in lista:
            addd = codd(+r[0])+desc(r[1])+tel(r[2])
            self.listBox1.Append(addd)
                   
    def OnLimparBtnButton(self, event):
        self.txtPesquisa.SetValue('')
        self.listBox1.Clear()

    
